//
//  LtqCucleImage.m
//  UI16_CycleImageByCollectionView
//
//  Created by Hanlingyu on 16/8/4.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "LtqCucleImage.h"
#import "LtqCycleImageCollectionViewCell.h"
#define kWidth self.bounds.size.width
#define kCollectionViewReuse @"reuse"
@interface LtqCucleImage ()
<
    UICollectionViewDelegate,
    UICollectionViewDataSource
>

@property (nonatomic, strong) UICollectionView *collection;


@end


@implementation LtqCucleImage

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        _imageArray = [NSMutableArray array];
        [self createCollectionView];
        
    }
    return self;
}

- (void)setImagesWithArray:(NSArray *)array {
    // 1. 根据传过来的数组, 进行处理, 生成图片数组
    NSArray *imagesArray = [self handleArray:array];
    // 2. 逻辑判断时, 需要用到数组内容
    self.imageArray = [NSArray arrayWithArray:imagesArray];
    // 3. 根据数组生成对应的ImageView
    [self createAllImageViews:imagesArray];
//    _imagesPage.numberOfPages = array.count;
    _collection.contentSize = CGSizeMake(self.bounds.size.width * imagesArray.count, 0);
    // 因为添加了最后一张, 要让轮播图看起来在第一张
    _collection.contentOffset = CGPointMake(self.bounds.size.width, 0);
}

// 使用数组创建ImageView
- (void)createAllImageViews:(NSArray *)imagesArray {
    // 逻辑判断用到数组属性
    // 直接使用数组创建imageView
    for (int i = 0; i < imagesArray.count; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.bounds.size.width, 0, self.bounds.size.width, self.bounds.size.height)];
        imageView.image = imagesArray[i];
        [_collection addSubview:imageView];
    }
}


- (NSArray *)handleArray:(NSArray *)array {
    // 判断类型(不知道别人使用你的类的时候传进来的类型是什么)
    id firstObject = [array firstObject];
    // 最终的图片数组
    NSMutableArray *imagesArray = [NSMutableArray array];
    // 判断对象是不是某种类型 isKindOfClass
    if ([firstObject isKindOfClass:[NSString class]]) {
        // 把字符串转成 UIImgae数组
        for (NSString *imageName in array) {
            UIImage *image = [UIImage imageNamed:imageName];
            [imagesArray addObject:image];
        }
    } else if ([firstObject isKindOfClass:[UIImage class]]) {
        // 把数组等于可变数组
        [imagesArray setArray:array];
    }
    // 获取到第一张图片
    UIImage *firstImage = [imagesArray firstObject];
    // 获取到最后一张图片
    UIImage *lastImage = [imagesArray lastObject];
    // 下标为0的位置放置最后一张图片
    [imagesArray insertObject:lastImage atIndex:0];
    // 在最后添加第一张图
    [imagesArray addObject:firstImage];
    // 把可变数组copy一份就是不可变的
    return [imagesArray copy];
}

- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(self.bounds.size.width, 500);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 500) collectionViewLayout:layout];
    _collection.backgroundColor = [UIColor whiteColor];
    _collection.pagingEnabled = YES;
    [self addSubview:_collection];
    
    _collection.delegate = self;
    _collection.dataSource = self;
    
    [_collection registerClass:[LtqCycleImageCollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewReuse];

    
    
    
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _imageArray.count + 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LtqCycleImageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.imageView.image = _imageArray.lastObject;
    } else if (indexPath.row == _imageArray.count + 1) {
        
        cell.imageView.image = _imageArray.firstObject;
    } else {
        
        cell.imageView.image = _imageArray[indexPath.row - 1];
        
    }

    
    return cell;
    
    
}

- (void)scrollViewDidEndDecelerating:(UICollectionView *)collection {
    
    if (collection.contentOffset.x == (self.imageArray.count + 1) * kWidth) {
        collection.contentOffset = CGPointMake(kWidth, 0);
    }else if(collection.contentOffset.x == 0) {
        collection.contentOffset = CGPointMake(self.imageArray.count * kWidth, 0);
    }
    
    
    
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
